package com.everyline.baselibrary.base.activity

import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.blankj.utilcode.util.LogUtils
import com.everyline.baselibrary.base.viewmodel.BaseViewModel
import com.everyline.baselibrary.ext.getVmClazz
import com.everyline.baselibrary.network.manager.NetState
import com.everyline.baselibrary.network.manager.NetworkStateManager
import com.everyline.baselibrary.view.MultipleStatusView
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.tbruyelle.rxpermissions3.RxPermissions

/**
 *creatd by 天天挂线
 *email:746608431@qq.com
 *on 2020/7/6
 *描述:TODO
 */
abstract class BaseVMDbActivity<VM : BaseViewModel, DB : ViewDataBinding> : AppCompatActivity() {
    var mLayoutStatusView: MultipleStatusView? = null
    lateinit var mViewModel: VM
    lateinit var mViewBinding: DB


    protected val mRxPermissions: RxPermissions by lazy { RxPermissions(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding = DataBindingUtil.setContentView(this, setLayoutId())
        mViewBinding.lifecycleOwner = this
        init(savedInstanceState)

    }


    private fun init(savedInstanceState: Bundle?) {
        initLocation()
        initView(savedInstanceState)
        createViewModel()

        getTitleBar()
        registerUiChange()

        createObserver()
        initData()

        initListener()

        retryStart()

        initRetryClickListener() //多种状态切换的view 重试点击事件

        //统一的titlebar
        if (getTitleBar() != 0) {
            val titleBar = findViewById<TitleBar>(getTitleBar())
            titleBar.setOnTitleBarListener(object : OnTitleBarListener {
                override fun onLeftClick(v: View?) {
                    onLeftClickListener(v)
                }

                override fun onRightClick(v: View?) {
                    onRightClickListener(v)
                }

                override fun onTitleClick(v: View?) {

                }

            })
        }


        //监听网络转态变化
        NetworkStateManager.instance.mNetworkStateCallback.observeInActivity(this, Observer {
            onNetworkStateChanged(it)
        })

    }


    private fun initRetryClickListener() {
        mLayoutStatusView?.setOnRetryClickListener(mRetryClickListener)
    }


    open val mRetryClickListener: View.OnClickListener = View.OnClickListener {
        retryStart()
    }

    private fun registerUiChange() {

        mViewModel.loadingChange.showDialog.observeInActivity(this, Observer {
            showLoading()
        })
        mViewModel.loadingChange.dismissDialog.observeInActivity(this, Observer {
            dismissLoading()
        })

    }


    /**
     * 创建viewModel
     */
    private fun createViewModel() {
        mViewModel = ViewModelProvider(this).get(getVmClazz(this))

    }

    abstract fun setLayoutId(): Int
    abstract fun getTitleBar(): Int
    abstract fun initView(savedInstanceState: Bundle?)
    abstract fun createObserver()
    abstract fun initData()
    open fun initLocation() {}
    abstract fun initListener()
    open fun retryStart() {}
    open fun onRightClickListener(v: View?) {}
    open fun onNetworkStateChanged(it: NetState) {}

    open fun onLeftClickListener(v: View?) {
        finish()
    }

    abstract fun showLoading(message: String = "请求网络中...")

    abstract fun dismissLoading()

    /**
     * 将非该Activity绑定的ViewModel添加 loading回调 防止出现请求时不显示 loading 弹窗bug
     * @param viewModels Array<out BaseViewModel>
     */
    protected fun addLoadingObserve(vararg viewModels: BaseViewModel) {
        viewModels.forEach { viewModel ->
            //显示弹窗
            viewModel.loadingChange.showDialog.observeInActivity(this, Observer {
                showLoading(it)
            })
            //关闭弹窗
            viewModel.loadingChange.dismissDialog.observeInActivity(this, Observer {
                dismissLoading()
            })
        }
    }


}
